#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>

int SetNonblocking(int fd) {
  if (fd < 0) return -1;
  int opts;
  if ((opts = fcntl(fd, F_GETFL)) != -1) {
    opts = opts | O_NONBLOCK;
    if(fcntl(fd, F_SETFL, opts) != -1) {
      return 0;
    }
  }
  return -1;
}